package com.linkstec.keyrobot;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

public class MainWindow extends JFrame{
    /*final int windowWidth = 210;
    final int windowHeight = 131;*/
    final int windowWidth = 400;
    final int windowHeight = 300;
    
    protected MenuItem open , exit;
    protected PopupMenu popup;
    protected TrayIcon trayIcon;
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    public MainWindow(){
        
        try {
            //正常
            BeautyEyeLNFHelper.frameBorderStyle = 
                    BeautyEyeLNFHelper.FrameBorderStyle.osLookAndFeelDecorated;
            BeautyEyeLNFHelper.launchBeautyEyeLNF();
        } catch (Exception e) {
        }
        
        super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        super.setTitle("路哥屏保杀手");
        ImageIcon logoImage = new ImageIcon(
        		this.getClass().getResource("/images/logo.jpg"));
        super.setIconImage(logoImage.getImage());
//        super.setSize(210, 130);
        super.setSize(400, 300);
        //设置背景图
        super.setContentPane(new JPanel(){
            private static final long serialVersionUID = 1L;
            @Override
            protected void paintComponent(Graphics g) {
                ImageIcon icon = new ImageIcon(
                		this.getClass().getResource("/images/desktop.jpg"));
                
                int width = icon.getIconWidth();
                int height = icon.getIconHeight();
                int left = (windowWidth - width) / 2;
                int top = (windowHeight - height) / 2;
                Image image = icon.getImage();
                g.drawImage(image, left, top, null);
            }
            
        });
        
        initMenu();
        
        //启动键盘按下事件
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){
                    //无限执行
                    KeyRobot.pressKey();
                }
            }
        }).start();
        
        //最小化至系统托盘
//        this.addWindowListener(new TrayIconAdapter(MainWindow.this));
        /*SystemTray tray = SystemTray.getSystemTray();
        try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			e.printStackTrace();
		}*/
        
        super.setLocationRelativeTo(null);
        super.setResizable(true);
        super.setVisible(true);
    }
    
    private void initMenu(){
        //初始化托盘图标
        popup = new PopupMenu();
        //打开
        open = new MenuItem();
        open.setLabel("打开");
        open.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                MainWindow.this.setVisible(true);
                MainWindow.this.setExtendedState(JFrame.NORMAL);
            }
        });
        popup.add(open);
        //退
        exit = new MenuItem();
        exit.setLabel("退出");
        exit.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        popup.add(exit);
    }
    
    public static void main(String[] args) {
        new MainWindow();
        URL picUrl = MainWindow.class.getClass().getResource("/images/desktop.jpg");

        String sss = picUrl.getPath();
        System.out.println(sss);
        System.out.println("HELLO");
        
    }
    
}